<template>
  <div class="app-container">
    <div>
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query.queryMap"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <!-- <el-button v-show="true" @click="print">打印</el-button> -->
          <template v-slot:filter-print>
            <PopoverPrint
              ref="popoverShow"
              v-model:popoverVisible="popoverVisible"
              :canPrint="true"
              :templateArr="templateArr"
              @tempClick="tempClick"
              @print="print"
            ></PopoverPrint>

          </template>
          <el-button v-show="true" @click="turnForward">结转</el-button>
          <!-- <el-button v-show="true" @click="exportExcel"
              >导出excel</el-button -->
          >
        </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="notAuditOrder"
        :tab-index="true"
        :check-box="true"
        v-model:query="filterInfo.query.queryMap"
        :field-list="tableInfo.fieldList"
        @handleEvent="handleEvent"
        @pageInfo="pageInfo"
      ></TableContainer>
    </div>

    <PageDialog
      v-if="dialogTipsInfo.visible"
      :title="dialogTipsInfo.title"
      v-model:visible="dialogTipsInfo.visible"
      :width="dialogTipsInfo.width"
      :bt-loading="dialogTipsInfo.btLoading"
      :bt-list="dialogTipsInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ dialogTipsInfo.msg }}：
        </div>
        <el-radio-group v-model="dialogTipsInfo.flag">
          <el-radio :label="true">当前页数据</el-radio>
          <el-radio :label="false">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </PageDialog>
  </div>
</template>

<script setup name="notAuditOrderList">
import { notAuditOrder, notAuditOrderMoveMonth } from "@/api/asset.js";
import { ElMessage } from 'element-plus'
import {
  exportInstitutionalInquiries,
  printInstitutionalInquiries,
} from "@/api/15189.js";

import { fatchTemplateByCode } from "@/api/base";
import {selectByCodeAndDept} from "@/api/report";
import {doPrint} from "@/utils/exportUtil";
const { proxy } = getCurrentInstance();
const page = ref({
  page: true,
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
});
const popoverVisible = ref(false)
const templateArr = ref([])
const dialogTipsInfo = ref({
  visible: false,
  title: "提示",
  btLoading: false,
  type: "",
  msg: "",
  width: "400px",
  flag: true,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "打印",
      type: "primary",
      event: checkConfirm,
      show: true,
    },
  ],
});
const filterInfo = ref({
  query: {},
  searchList: [],
  searchBtn: [{ type: "button", label: "搜索", event: search }],
  btnList: [
    {
      type: "button",
      label: "打印",
      event: print,
      show: proxy.$permission("notAuditOrderList:print"),
    },
    //   是否全部执行结转下月
    {
      type: "button",
      label: "结转",
      event: turnForward,
      show: proxy.$permission("notAuditOrderList:moveMonth"),
    },
    //   {
    //     type: 'button',
    //     label: '导出excel',
    //     event: exportExcel,
    //     show: proxy.$permission('notAuditOrderList:execl'),
    //   },
  ],
});
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [
    { label: "业务类型", value: "orderTypeValue" },
    { label: "单据号", value: "orderNo" },
    { label: "制单日期", value: "businessDate" },
    { label: "制单人", value: "operateUserName" },
    { label: "单据状态", value: "orderStatusValue" },
  ],
});
function getList() {
  tableInfo.value.refresh = Math.random();
}
function search() {
  tableInfo.value.initCurpage = Math.random();
  tableInfo.value.refresh = Math.random();
}
function handleEvent(event, data) {
  switch (event) {
    case "tableCheck":
      tableInfo.value.selections = [...data];
      break;
  }
}

function turnForward() {
  // 没勾选提示
  if (!tableInfo.value.selections.length) {
    // 是否全部执行结转下月
    proxy
      .$confirm("是否全部执行结转下月", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
      .then(({ value }) => {
        notAuditOrderMoveMonth([]).then(res =>{
          if(res && res.success) {
            ElMessage.success({
              message: '操作成功',
            })
            getList()
          }
        })
      });
  } else {
      notAuditOrderMoveMonth(tableInfo.value.selections).then(res =>{
        if(res && res.success) {
            ElMessage.success({
              message: '操作成功',
            })
            getList()
          }
      })
  }
}

function exportExcelList() {
  let params = {
    ...filterInfo.value.query.queryMap,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  };
  exportInstitutionalInquiries(params).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp);
    }
  });
}
async function print() {
  selectByCodeAndDept({
    tempCode: 'notAuditOrderList',
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        console.log(proxy.$refs.popoverShow)
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            ...filterInfo.value.query.queryMap,
            ...page.value,
          },
          callback: printCallback,
          data: {},
        })
      }
    }
  })
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      ...filterInfo.value.query,
    },
    callback: printCallback,
    data: {},
  })
  popoverVisible.value = false
}
function printCallback() {
  try {
    // 打印成功增加打印次数
    // updatePrintCount({
    //   orderNo: infoList.value.data.orderNo,
    //   orderId: props.orderId,
    //   tempCode: 'paymentDeatil',
    //   orderType: 0,
    // })
  } catch (error) {}
}
function exportExcel() {
  //导出
  dialogTipsInfo.value.visible = true;
  dialogTipsInfo.value.type = "excel";
  dialogTipsInfo.value.msg = "请选择数据导出范围";
  dialogTipsInfo.value.btList[1].label = "导出";
  dialogTipsInfo.value.flag = true;
}
//打印导出
function pageInfo(obj) {
  page.value = obj;
}
function checkConfirm() {
  if (dialogTipsInfo.value.type === "excel") {
    exportExcelList();
  } else if (dialogTipsInfo.value.type === "print") {
    printList();
  }
  dialogTipsInfo.value.visible = false;
  dialogTipsInfo.value.msg = "";
}
function handleClose() {
  dialogTipsInfo.value.visible = false;
}

let obj = {
  orderTypeValue: {
    label: "业务类型",
    value: "orderTypeValue",
    asValue: "orderType",
    componentType: "select-model",
    code: "asset_order_move_type",
    event: search,
    show: true,
    hidden: false,
  },
  nameLike: {
    label: "单据号",
    value: "orderNo",
    type: "column",
    componentType: "input",
    event: search,
    hidden: false,
    show: true,
  },
};
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(obj),
});

onMounted(() => {
  // getList();
});
</script>

<style scoped></style>
